package com.hmall.controller.back;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.google.gson.Gson;
import com.hdmall.business.common.annotation.AuthRequired;
import com.hdmall.business.common.common.ServerResponse;

import com.hmall.mapper.*;

import com.hmall.model.*;


import com.hdmall.business.common.util.HttpClientUtil;
import com.hdmall.business.common.util.NumberUtil;
import com.hmall.util.TokenUtil;
import com.hmall.vo.*;

import java.util.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.math.BigDecimal;
import java.math.RoundingMode;

import java.text.SimpleDateFormat;
import java.util.Date;

import static java.math.BigDecimal.ROUND_HALF_DOWN;


@RestController
@RequestMapping("/manage/order")
@Api(value = "后台订单管理操作API", description = "后台订单管理类API说明")
public class OrderManageController {
    private final
    MallOrderMapper mallOrderMapper;
    private final
    MallOrderItemMapper mallOrderItemMapper;
    private final
    MallShippingMapper mallShippingMapper;
    private final
    MallCommentMapper mallCommentMapper;
    private final
    MallCommentDetailMapper mallCommentDetailMapper;
    private final
    MallProductMapper mallProductMapper;
    private final
    SysUserMapper sysUserMapper;
    private final
    MallPriceMapper mallPriceMapper;
    private final
    MallShopMapper mallShopMapper;
    private final
    MallPaymentVoucherMapper mallPaymentVoucherMapper;
    private final
    MallPaymentUnpayMapper mallPaymentUnpayMapper;
    private final
    MallOrderFufillmentMapper mallOrderFufillmentMapper;
    private final
    SysCustomerMapper sysCustomerMapper;
    private final
    MallDealerMapper mallDealerMapper;
    private final
    MallOrderFufillmentParentMapper mallOrderFufillmentParentMapper;
    private final
    JedisPool jedisPool;
    private final
    SysUserDeptMapper sysUserDeptMapper;
    private final
    MallProductPlanItemMapper mallProductPlanItemMapper;
    private final
    SysCustomerCreditMapper sysCustomerCreditMapper;
    private final
    SysCustomerStatementMapper sysCustomerStatementMapper;
    private final
    MallOrderRemarkMapper mallOrderRemarkMapper;
    private final
    MallOrderItemDetailMapper mallOrderItemDetailMapper;
    private final
    SysCustomerDetailMapper sysCustomerDetailMapper;
    private final
    MallProductForwardStockMapper mallProductForwardStockMapper;
    private final
    DictionMapper dictionMapper;
    private final
    SysUserExtraMapper sysUserExtraMapper;
    private final
    BiUserAnalysisMapper biUserAnalysisMapper;

    private TokenUtil tokenUtil = new TokenUtil();

    @Autowired
    public OrderManageController(MallCommentMapper mallCommentMapper, MallOrderMapper mallOrderMapper, MallOrderItemMapper mallOrderItemMapper, SysCustomerCreditMapper sysCustomerCreditMapper, SysCustomerMapper sysCustomerMapper, MallOrderFufillmentParentMapper mallOrderFufillmentParentMapper, MallProductForwardStockMapper mallProductForwardStockMapper, JedisPool jedisPool, SysUserDeptMapper sysUserDeptMapper, MallShippingMapper mallShippingMapper, MallOrderFufillmentMapper mallOrderFufillmentMapper, MallPaymentVoucherMapper mallPaymentVoucherMapper, BiUserAnalysisMapper biUserAnalysisMapper, MallCommentDetailMapper mallCommentDetailMapper, SysUserExtraMapper sysUserExtraMapper, MallProductMapper mallProductMapper, SysUserMapper sysUserMapper, MallDealerMapper mallDealerMapper, SysCustomerDetailMapper sysCustomerDetailMapper, MallPriceMapper mallPriceMapper, MallOrderRemarkMapper mallOrderRemarkMapper, MallOrderItemDetailMapper mallOrderItemDetailMapper, MallProductPlanItemMapper mallProductPlanItemMapper, SysCustomerStatementMapper sysCustomerStatementMapper, DictionMapper dictionMapper, MallShopMapper mallShopMapper, MallPaymentUnpayMapper mallPaymentUnpayMapper) {
        this.mallCommentMapper = mallCommentMapper;
        this.mallOrderMapper = mallOrderMapper;
        this.mallOrderItemMapper = mallOrderItemMapper;
        this.sysCustomerCreditMapper = sysCustomerCreditMapper;
        this.sysCustomerMapper = sysCustomerMapper;
        this.mallOrderFufillmentParentMapper = mallOrderFufillmentParentMapper;
        this.mallProductForwardStockMapper = mallProductForwardStockMapper;
        this.jedisPool = jedisPool;
        this.sysUserDeptMapper = sysUserDeptMapper;
        this.mallShippingMapper = mallShippingMapper;
        this.mallOrderFufillmentMapper = mallOrderFufillmentMapper;
        this.mallPaymentVoucherMapper = mallPaymentVoucherMapper;
        this.biUserAnalysisMapper = biUserAnalysisMapper;
        this.mallCommentDetailMapper = mallCommentDetailMapper;
        this.sysUserExtraMapper = sysUserExtraMapper;
        this.mallProductMapper = mallProductMapper;
        this.sysUserMapper = sysUserMapper;
        this.mallDealerMapper = mallDealerMapper;
        this.sysCustomerDetailMapper = sysCustomerDetailMapper;
        this.mallPriceMapper = mallPriceMapper;
        this.mallOrderRemarkMapper = mallOrderRemarkMapper;
        this.mallOrderItemDetailMapper = mallOrderItemDetailMapper;
        this.mallProductPlanItemMapper = mallProductPlanItemMapper;
        this.sysCustomerStatementMapper = sysCustomerStatementMapper;
        this.dictionMapper = dictionMapper;
        this.mallShopMapper = mallShopMapper;
        this.mallPaymentUnpayMapper = mallPaymentUnpayMapper;
    }

    @AuthRequired
    @PostMapping("saveWayBill")
    @ApiOperation(value = "保存运单号")
    public ServerResponse<String> saveWayBill(Integer id, String waybill_no,
                                              String logistics_company) {
        MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(id);
        mallOrder.setLogisticsCompany(logistics_company);
        mallOrder.setWaybillNo(waybill_no);
        mallOrderMapper.updateByPrimaryKey(mallOrder);

        return ServerResponse.createBySuccess("保存成功");
    }

    @AuthRequired
    @GetMapping("GetSampleOrderList")
    @ApiOperation(value = "拿样订单列表")
    public ServerResponse<PageInfo<MallOrderManageVo>> GetSampleOrderList(Integer dept_id, Integer status,
                                                                          @RequestParam(value = "pageNum", defaultValue = "1")
                                                                                  int pageNum,
                                                                          @RequestParam(value = "pageSize", defaultValue = "100")
                                                                                  int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        Integer shop_id = mallShopMapper.selectByDeptId(dept_id);
        List<MallOrderManageVo> orderManageVoList;

        if (shop_id == null) {
            orderManageVoList = mallOrderMapper.selectSampleDealerOrder(status, dept_id);
        } else {
            orderManageVoList = mallOrderMapper.selectSampleOrder(status, dept_id);
        }

        for (MallOrderManageVo anOrderManageVoList : orderManageVoList) {
            MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(anOrderManageVoList.getId());
            List<MallOrderItemManageVo> mallOrderItemMappers = mallOrderItemMapper.selectByOrderNo(mallOrder.getOrderNo());
            anOrderManageVoList.setProductList(mallOrderItemMappers);
        }

        PageInfo<MallOrderManageVo> pageResult = new PageInfo<>(orderManageVoList);

        return ServerResponse.createBySuccess(pageResult);
    }

    @AuthRequired
    @GetMapping("GetSalesManSampleOrderList")
    @ApiOperation(value = "业务员拿样订单列表")
    public ServerResponse<PageInfo<MallOrderManageVo>> GetSalesManSampleOrderList(Integer status, Integer user_id,
                                                                                  @RequestParam(value = "pageNum", defaultValue = "1")
                                                                                          int pageNum,
                                                                                  @RequestParam(value = "pageSize", defaultValue = "100")
                                                                                          int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<MallOrderManageVo> orderManageVoList;

        orderManageVoList = mallOrderMapper.selectSalesManSampleDealerOrder(status, user_id);

        for (MallOrderManageVo anOrderManageVoList : orderManageVoList) {
            MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(anOrderManageVoList.getId());
            List<MallOrderItemManageVo> mallOrderItemMappers = mallOrderItemMapper.selectByOrderNo(mallOrder.getOrderNo());
            anOrderManageVoList.setProductList(mallOrderItemMappers);
        }

        PageInfo<MallOrderManageVo> pageResult = new PageInfo<>(orderManageVoList);

        return ServerResponse.createBySuccess(pageResult);
    }


    @AuthRequired
    @GetMapping("GetMyOrderList")
    @ApiOperation(value = "业务员订单")
    public ServerResponse<PageVo<MallOrderManageVo>> GetMyOrderList(Integer status, Integer user_id,
                                                                    @RequestParam(value = "pageNum", defaultValue = "1")
                                                                            int pageNum,
                                                                    @RequestParam(value = "pageSize", defaultValue = "100")
                                                                            int pageSize) {
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;

        List<MallOrderManageVo> orderManageVoList;
        total = mallOrderMapper.selectMyDealerOrderCount(status, user_id);
        orderManageVoList = mallOrderMapper.selectMyDealerOrder(status, user_id, pageStart, pageSize);

        for (MallOrderManageVo anOrderManageVoList : orderManageVoList) {
            MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(anOrderManageVoList.getId());
            List<MallOrderItemManageVo> mallOrderItemMappers = mallOrderItemMapper.selectByOrderNo(mallOrder.getOrderNo());
            anOrderManageVoList.setProductList(mallOrderItemMappers);
        }

        PageVo<MallOrderManageVo> pageVo = new PageVo<>(pageNum, pageSize, total, orderManageVoList);


        return ServerResponse.createBySuccess(pageVo);
    }

    @AuthRequired
    @GetMapping("GetOrderManageList")
    @ApiOperation(value = "跟单员订单")
    public ServerResponse GetOrderManageList(Integer status, Integer user_id, String order_no, String bus_name,
                                             @RequestParam(value = "pageNum", defaultValue = "1")
                                                     int pageNum,
                                             @RequestParam(value = "pageSize", defaultValue = "100")
                                                     int pageSize) {
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;

        List<MallOrderManageVo> orderManageVoList;

        SysUser sysUser = sysUserMapper.selectByUserId(user_id);
        Integer dept_id;
        if (sysUser == null) {
            return ServerResponse.createByErrorMessage("不是后端用户");
        } else {
            dept_id = sysUser.getDeptId();
        }
        if (dept_id == 11) {
            total = mallOrderMapper.selectMyTotalOrderCount(status, dept_id, order_no, bus_name);
            orderManageVoList = mallOrderMapper.selectMyTotalOrder(status, dept_id, order_no, bus_name, pageStart, pageSize);
        } else {
            total = mallOrderMapper.selectManageOrderCount(status, user_id, order_no, bus_name);
            orderManageVoList = mallOrderMapper.selectManageOrder(status, user_id, order_no, bus_name, pageStart, pageSize);
        }


        for (MallOrderManageVo anOrderManageVoList : orderManageVoList) {
            MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(anOrderManageVoList.getId());
            List<MallOrderItemManageVo> mallOrderItemMappers = mallOrderItemMapper.selectByOrderNo(mallOrder.getOrderNo());
            anOrderManageVoList.setProductList(mallOrderItemMappers);
        }

        PageVo<MallOrderManageVo> pageVo = new PageVo<>(pageNum, pageSize, total, orderManageVoList);

        return ServerResponse.createBySuccess(pageVo);
    }

    @AuthRequired
    @GetMapping("GetOrderList")
    @ApiOperation(value = "订单列表")
    public ServerResponse<PageVo<MallOrderManageVo>> GetOrderList(Integer dept_id, Integer status,
                                                                  @RequestParam(value = "pageNum", defaultValue = "1")
                                                                          int pageNum,
                                                                  @RequestParam(value = "pageSize", defaultValue = "100")
                                                                          int pageSize) {
        Integer pageStart = pageSize * (pageNum - 1);

        PageHelper.startPage(pageNum, pageSize);

        Integer shop_id = mallShopMapper.selectByDeptId(dept_id);
        List<MallOrderManageVo> orderManageVoList;
        Integer total;
        if (shop_id == null) {
            total = mallOrderMapper.selectDealerOrderCount(status,
                    dept_id);
            orderManageVoList = mallOrderMapper.selectDealerOrder(status,
                    dept_id, pageStart, pageSize);
        } else {
            total = mallOrderMapper.selectTotalOrderCount(status,
                    dept_id);
            orderManageVoList = mallOrderMapper.selectTotalOrder(status, dept_id, pageStart, pageSize);
        }
        System.out.println(orderManageVoList);

        for (MallOrderManageVo anOrderManageVoList : orderManageVoList) {
            MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(anOrderManageVoList.getId());
            List<MallOrderItemManageVo> mallOrderItemMappers = mallOrderItemMapper.selectByOrderNo(mallOrder.getOrderNo());
            anOrderManageVoList.setProductList(mallOrderItemMappers);
        }
        System.out.println(pageNum);
        System.out.println(pageSize);
        System.out.println(total);
        System.out.println(orderManageVoList);

        PageVo<MallOrderManageVo> pageVo = new PageVo<>(pageNum, pageSize, total, orderManageVoList);


        return ServerResponse.createBySuccess(pageVo);
    }

    @AuthRequired
    @PostMapping("CancelOrder")
    @ApiOperation(value = "取消订单")
    public ServerResponse CancelOrder(Integer id) {
        Integer count = mallOrderMapper.cancelOrder(id);

        if (count == 0) {
            return ServerResponse.createByErrorMessage("修改订单失败");
        } else {
            return ServerResponse.createBySuccess("修改订单成功");
        }
    }

    @AuthRequired
    @DeleteMapping("DeleteOrder")
    @ApiOperation(value = "删除订单")
    public ServerResponse DeleteOrder(Integer[] id) {
        int order_id;
        int count = 0;

        for (Integer anId : id) {
            order_id = anId;

            Long orderNO = mallOrderMapper.selectByPrimaryKey(order_id)
                    .getOrderNo();
            mallOrderItemMapper.deleteByOrderNo(orderNO);
            count = count + mallOrderMapper.deleteByPrimaryKey(order_id);
        }

        if (count == 0) {
            return ServerResponse.createByErrorMessage("删除失败");
        } else {
            return ServerResponse.createBySuccess("删除成功");
        }
    }

    @AuthRequired
    @PostMapping("DeleteOrderItem")
    @ApiOperation(value = "删除订单产品")
    public ServerResponse DeleteOrderItem(Integer id) {
        BigDecimal change_price = mallOrderItemMapper.selectByPrimaryKey(id)
                .getTotalPrice();
        change_price = change_price.multiply(new BigDecimal(-1));

        Integer orderId = mallOrderItemMapper.selectOrderId(id);
        mallOrderMapper.updatePayment(orderId, change_price);

        Integer count = mallOrderItemMapper.deleteByPrimaryKey(id);

        if (count == 0) {
            return ServerResponse.createByErrorMessage("删除订单产品失败");
        } else {
            return ServerResponse.createBySuccess("删除订单产品成功");
        }
    }

    @AuthRequired
    @PostMapping("SaveOrder")
    @ApiOperation(value = "保存订单")
    public ServerResponse<String> SaveOrder(Integer id, Integer shipping_id,
                                            String distName) {
        System.out.println(shipping_id);

        Integer distCode;

        if (distName.equals("自提")) {
            distCode = 1;
        } else {
            distCode = 2;
        }

        mallOrderMapper.updateOrder(id, shipping_id, distCode);

        return ServerResponse.createBySuccess("保存成功");
    }

    @AuthRequired
    @PostMapping("SaveSampleItem")
    @ApiOperation(value = "保存拿样单产品")
    public ServerResponse<String> SaveSampleItem(Integer id, Integer quantity) {
        mallOrderItemMapper.updateQuantity(id, quantity);

        return ServerResponse.createBySuccess("保存成功");
    }

    @AuthRequired
    @PostMapping("SaveOrderItem")
    @ApiOperation(value = "保存订单产品")
    public ServerResponse<String> SaveOrderItem(Integer id,
                                                BigDecimal current_unit_price, Integer quantity) {
        BigDecimal total_price = current_unit_price.multiply(new BigDecimal(
                quantity));
        total_price = total_price.setScale(2, RoundingMode.HALF_UP);

        MallOrderItem mallOrderItem = mallOrderItemMapper.selectByPrimaryKey(id);
        BigDecimal change_price = total_price.subtract(mallOrderItem.getTotalPrice());
        mallOrderItemMapper.updateOrderItem(id, current_unit_price, quantity,
                total_price);

        Integer orderId = mallOrderItemMapper.selectOrderId(id);
        mallOrderMapper.updatePayment(orderId, change_price);

        return ServerResponse.createBySuccess("保存成功");
    }

    @AuthRequired
    @PostMapping("findShippingList")
    @ApiOperation(value = "查询收货地址")
    public ServerResponse<List<MallShippingVo>> findShippingList(String username) {
        List<MallShippingVo> list = mallShippingMapper.selectByUserName(username);

        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @PostMapping("findGrade")
    @ApiOperation(value = "查询等级")
    public ServerResponse<List<GradeVo>> findGrade(Integer product_id) {
        List<GradeVo> list = mallPriceMapper.selectGrade(product_id);

        return ServerResponse.createBySuccess(list);
    }

    //    @AuthRequired
    @PostMapping("passOrder")
    @ApiOperation(value = "通过审核")
    public ServerResponse passOrder(Integer id, String opinion, Long order_no, Integer user_id, Integer radio) {
        String params1 = "";
        String params2 = "2";
        MallOrder mallOrder3 = mallOrderMapper.selectByOrderNo(order_no);
        Integer customer_id2 = sysCustomerMapper.selectCustomerId(mallOrder3.getUserId());
        SysCustomer sysCustomer = sysCustomerMapper.selectByPrimaryKey(customer_id2);
        SysUser testUser = sysUserMapper.selectByPrimaryKey(sysCustomer.getSalesmanId());
        if (testUser.getUsername().contains("演示")) {
            return ServerResponse.createByErrorMessage("演示账号不能下单");
        }
        List<MallOrderItem> list = mallOrderItemMapper.getByOrderNo(order_no);
        for (MallOrderItem aList : list) {
            List<MallOrderItemDetail> list1 = mallOrderItemDetailMapper.selectByItemId(aList.getId());
            if (list1.size() == 0) {
                return ServerResponse.createByErrorMessage(aList.getProductModel() + "产品未选择物料编码");
            }
            for (MallOrderItemDetail mallOrderItemDetail : list1) {
                params1 = params1 + mallOrderItemDetail.getMaterialCode() + "-" + mallOrderItemDetail.getGrade()
                        + "-" + mallOrderItemDetail.getTwisting() + "-" + mallOrderItemDetail.getColor() + "-" + mallOrderItemDetail.getMachineNo() + "-" + mallOrderItemDetail.getCheckQuantity().intValue() + ",";
            }
        }
        Integer count = mallOrderItemDetailMapper.checkSelect(order_no);
        if (count != 0) {
            return ServerResponse.createByErrorMessage("有物料编码未分组");
        }
        MallOrder mallOrder = mallOrderMapper.selectByOrderNo(order_no);
        Integer customer_id = sysCustomerMapper.selectCustomerId(mallOrder.getUserId());
        //更改库存
        params1 = params1.substring(0, params1.length() - 1);
        Map map2 = new HashMap();
        System.out.println(params1);
        System.out.println(params2);
        System.out.println(testUser.getDeptId().toString());
        map2.put("itemList", params1);
        map2.put("type", params2);
        map2.put("deptId", testUser.getDeptId().toString());
        Diction diction = dictionMapper.selectByDictionKey("wmsSwitch");
        if (diction != null && diction.getValue().equals("1")) {
            String s = HttpClientUtil.doPostWithParam("http://47.98.171.71:9600/wms/inventorys/batch/updateInventory", map2);
            Gson gson = new Gson();
            Map map3 = new HashMap();
            map3 = gson.fromJson(s, map3.getClass());
            System.out.println(s);
            System.out.println(map3.get("code").toString());
            if (!map3.get("code").toString().equals("200.0")) {
                return ServerResponse.createByErrorMessage(map3.get("message").toString());
            }
        }
//        SysCustomerCredit sysCustomerCredit = sysCustomerCreditMapper.selectByCustomerId(customer_id);
//        if (sysCustomerCredit == null) {
//            mallOrderMapper.updateByOrderNo(30, order_no);
//        }else {
//            if ((sysCustomerCredit.getQuota()==0&&sysCustomerCredit.getAccountPeriod()==0)){
//                mallOrderMapper.updateByOrderNo(30, order_no);
//            }else {
//                mallOrderMapper.updateByOrderNo(20, order_no);
//            }
//        }
        if (radio == 1) {
            mallOrder.setStatus1(1);
            mallOrder.setStatus2(0);
            mallOrder.setStatus3(0);
            mallOrderMapper.updateByPrimaryKey(mallOrder);
        } else {
            mallOrder.setStatus1(1);
            mallOrder.setStatus2(1);
            mallOrder.setStatus3(0);
            mallOrder.setStatus(20);
            mallOrderMapper.updateByPrimaryKey(mallOrder);
            SysCustomerCredit sysCustomerCredit2 = sysCustomerCreditMapper.selectByCustomerId(customer_id);
            method1(mallOrder, customer_id, sysCustomerCredit2);
        }


//        SysCustomerStatement sysCustomerStatement = sysCustomerStatementMapper.selectByCustomerIdAndTime(customer_id,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1);
//        if (sysCustomerStatement != null) {
//            sysCustomerStatement.setThisMonthUnpayment(sysCustomerStatement.getThisMonthUnpayment().add(mallOrder.getPayment()));
//            sysCustomerStatementMapper.updateByPrimaryKey(sysCustomerStatement);
//        } else {
//            SysCustomerStatement sysCustomerStatement3 = new SysCustomerStatement();
//            sysCustomerStatement3.setCustomerId(customer_id);
//            sysCustomerStatement3.setYear(calendar.get(Calendar.YEAR));
//            sysCustomerStatement3.setMonth(calendar.get(Calendar.MONTH)+1);
//            SysCustomerStatement sysCustomerStatement2 = sysCustomerStatementMapper.selectByCustomerIdAndTime(customer_id,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH));
//            if (sysCustomerStatement2 == null){
//                sysCustomerStatement3.setLastMonthUnpayment(new BigDecimal(0));
//            }else {
//                sysCustomerStatement3.setLastMonthUnpayment(sysCustomerStatement2.getThisMonthUnpayment());
//            }
//            sysCustomerStatement3.setThisMonthReturnPayment(new BigDecimal(0));
//            sysCustomerStatement3.setThisMonthFufillmentPayment(new BigDecimal(0));
//            sysCustomerStatement3.setThisMonthUnpayment(mallPaymentUnpay.getUnpayPayment());
//            sysCustomerStatementMapper.insert(sysCustomerStatement3);
//        }
        MallOrderRemark mallOrderRemark = new MallOrderRemark();
        mallOrderRemark.setContent(opinion);
        mallOrderRemark.setOrderNo(order_no);
        mallOrderRemark.setUserId(user_id);
        mallOrderRemark.setType(1);
        mallOrderRemark.setCreateTime(new Date());
        mallOrderRemarkMapper.insert(mallOrderRemark);
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", order_no.toString());
        HttpClientUtil.doPostWithParam("http://47.98.169.251:9094/redisMQ/tradeWarning", map);


        return ServerResponse.createBySuccess("审核成功");
    }

    @AuthRequired
    @PostMapping("wxStatement")
    @ApiOperation(value = "微信对账单")
    public ServerResponse wxStatement(String open_id, BigDecimal unpayment) {
        SysUserExtra sysUserExtra = sysUserExtraMapper.selectByWeChatOpenId(open_id);
        if (sysUserExtra == null) {
            return ServerResponse.createByErrorMessage("无此用户");
        }
        Integer user_id = sysUserExtra.getUserId();
        Integer customer_id = sysCustomerMapper.selectCustomerId(user_id);
        MallOrder mallOrder = new MallOrder();
        mallOrder.setPayment(unpayment);
        SysCustomerCredit sysCustomerCredit2 = sysCustomerCreditMapper.selectByCustomerId(customer_id);
        method1(mallOrder, customer_id, sysCustomerCredit2);
        return ServerResponse.createBySuccess("成功");
    }

    private void method1(MallOrder mallOrder, Integer customer_id, SysCustomerCredit sysCustomerCredit2) {
        if (sysCustomerCredit2 == null || (sysCustomerCredit2.getQuota() == 0 && sysCustomerCredit2.getAccountPeriod() == 0)) {
            MallPaymentUnpay mallPaymentUnpay = mallPaymentUnpayMapper.selectByCustomerId(customer_id);
            method2(mallOrder, customer_id, mallPaymentUnpay);

            //设置发货单数据
            Calendar calendar = Calendar.getInstance();
            SysCustomerStatement sysCustomerStatement = sysCustomerStatementMapper.selectByCustomerIdAndTime(customer_id, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1);
            BigDecimal unpayment = mallOrder.getPayment();
            method3(customer_id, calendar, sysCustomerStatement, unpayment);
        }
    }

    private void method3(Integer customer_id, Calendar calendar, SysCustomerStatement sysCustomerStatement, BigDecimal unpayment) {
        if (sysCustomerStatement != null) {
            sysCustomerStatement.setThisMonthFufillmentPayment(sysCustomerStatement.getThisMonthFufillmentPayment().add(unpayment));
            sysCustomerStatement.setThisMonthUnpayment(sysCustomerStatement.getThisMonthUnpayment().add(sysCustomerStatement.getThisMonthFufillmentPayment()));
            sysCustomerStatementMapper.updateByPrimaryKey(sysCustomerStatement);
        } else {
            SysCustomerStatement sysCustomerStatement3 = new SysCustomerStatement();
            sysCustomerStatement3.setCustomerId(customer_id);
            sysCustomerStatement3.setYear(calendar.get(Calendar.YEAR));
            sysCustomerStatement3.setMonth(calendar.get(Calendar.MONTH) + 1);
            SysCustomerStatement sysCustomerStatement2 = sysCustomerStatementMapper.selectByCustomerIdAndTime(customer_id, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH));
            if (sysCustomerStatement2 == null) {
                sysCustomerStatement3.setLastMonthUnpayment(new BigDecimal(0));
                sysCustomerStatement3.setThisMonthUnpayment(new BigDecimal(0));
            } else {
                sysCustomerStatement3.setLastMonthUnpayment(sysCustomerStatement2.getThisMonthUnpayment());
                sysCustomerStatement3.setThisMonthUnpayment(sysCustomerStatement2.getThisMonthUnpayment());
            }
            sysCustomerStatement3.setThisMonthReturnPayment(new BigDecimal(0));
            sysCustomerStatement3.setThisMonthUnpayment(sysCustomerStatement3.getThisMonthUnpayment().add(unpayment));
            sysCustomerStatement3.setThisMonthFufillmentPayment(unpayment);
            sysCustomerStatement3.setCreateTime(new Date());
            sysCustomerStatementMapper.insert(sysCustomerStatement3);
        }
    }

    private void method2(MallOrder mallOrder, Integer customer_id, MallPaymentUnpay mallPaymentUnpay) {
        if (mallPaymentUnpay == null) {
            MallPaymentUnpay mallPaymentUnpay1 = new MallPaymentUnpay();
            mallPaymentUnpay1.setCustomerId(customer_id);
            mallPaymentUnpay1.setUnpayPayment(mallOrder.getPayment());
            mallPaymentUnpayMapper.insert(mallPaymentUnpay1);
        } else {
            mallPaymentUnpay.setUnpayPayment(mallPaymentUnpay.getUnpayPayment().add(mallOrder.getPayment()));
            mallPaymentUnpayMapper.updateByPrimaryKey(mallPaymentUnpay);
        }
    }

    @AuthRequired
    @PostMapping("unpassOrder")
    @ApiOperation(value = "不通过审核")
    public ServerResponse<String> unpassOrder(Integer id, String opinion, Long order_no, Integer user_id) {
        mallOrderMapper.updateByOrderNo(0, order_no);
        MallOrder mallOrder = mallOrderMapper.selectByOrderNo(order_no);
        mallOrder.setExamCheck(1);
        mallOrder.setStatus1(2);
        mallOrderMapper.updateByPrimaryKey(mallOrder);

        MallOrderRemark mallOrderRemark = new MallOrderRemark();
        mallOrderRemark.setContent(opinion);
        mallOrderRemark.setOrderNo(order_no);
        mallOrderRemark.setUserId(user_id);
        mallOrderRemark.setType(1);
        mallOrderRemark.setCreateTime(new Date());
        mallOrderRemarkMapper.insert(mallOrderRemark);


        return ServerResponse.createBySuccess("审核成功");
    }

    @AuthRequired
    @GetMapping("SearchOrder")
    @ApiOperation(value = "搜索订单")
    public ServerResponse<PageInfo<MallOrderManageVo>> SearchOrder(String query, Integer type,
                                                                   Integer dept_id,
                                                                   @RequestParam(value = "pageNum", defaultValue = "1")
                                                                           int pageNum,
                                                                   @RequestParam(value = "pageSize", defaultValue = "100")
                                                                           int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<MallOrderManageVo> orderList;
        Integer shop_id = mallShopMapper.selectByDeptId(dept_id);

        if (shop_id == null) {
            orderList = mallOrderMapper.selectDealerList(query, type, dept_id);
        } else {
            orderList = mallOrderMapper.selectListByNo(query, type, dept_id);
        }

        for (MallOrderManageVo anOrderList : orderList) {
            MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(anOrderList.getId());
            List<MallOrderItemManageVo> mallOrderItemMappers = mallOrderItemMapper.selectByOrderNo(mallOrder.getOrderNo());
            anOrderList.setProductList(mallOrderItemMappers);
        }

        PageInfo<MallOrderManageVo> pageResult = new PageInfo<>(orderList);

        return ServerResponse.createBySuccess(pageResult);
    }

    @AuthRequired
    @GetMapping("SearchSampleOrder")
    @ApiOperation(value = "搜索拿样订单")
    public ServerResponse<PageInfo<MallOrderManageVo>> SearchOrder(String order_no, Integer dept_id,
                                                                   Integer status, String bus_name,
                                                                   @RequestParam(value = "pageNum", defaultValue = "1")
                                                                           int pageNum,
                                                                   @RequestParam(value = "pageSize", defaultValue = "100")
                                                                           int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<MallOrderManageVo> orderList;
        Integer shop_id = mallShopMapper.selectByDeptId(dept_id);

        if (shop_id == null) {
            orderList = mallOrderMapper.selectDealerSampleList(order_no, dept_id, status, bus_name);
        } else {
            orderList = mallOrderMapper.selectSampleListByNo(order_no, dept_id, status, bus_name);
        }

        for (MallOrderManageVo anOrderList : orderList) {
            MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(anOrderList.getId());
            List<MallOrderItemManageVo> mallOrderItemMappers = mallOrderItemMapper.selectByOrderNo(mallOrder.getOrderNo());
            anOrderList.setProductList(mallOrderItemMappers);
        }

        PageInfo<MallOrderManageVo> pageResult = new PageInfo<>(orderList);

        return ServerResponse.createBySuccess(pageResult);
    }

    @AuthRequired
    @GetMapping("viewComment")
    @ApiOperation(value = "查看评论")
    public ServerResponse<MallComment> viewComment(Integer item_id) {
        MallComment mallComment = mallCommentMapper.findByOrderItemId(item_id);

        return ServerResponse.createBySuccess(mallComment);
    }

    @AuthRequired
    @GetMapping("viewReply")
    @ApiOperation(value = "查看回复")
    public ServerResponse<List<MallCommentDetailVo>> viewReply(Integer comment_id) {
        List<MallCommentDetailVo> commentDetailVoList = mallCommentDetailMapper.selectByCommentId(comment_id);

        return ServerResponse.createBySuccess(commentDetailVoList);
    }

    @AuthRequired
    @PostMapping("reply")
    @ApiOperation(value = "回复")
    public ServerResponse<String> reply(@RequestBody
                                                MallCommentDetail mallCommentDetail) {
        MallComment mallComment = mallCommentMapper.selectByPrimaryKey(mallCommentDetail.getCommentId());
        mallComment.setCommentNum(mallComment.getCommentNum() + 1);
        mallCommentMapper.updateByPrimaryKey(mallComment);
        mallCommentDetail.setAnoymous(0);

        Date date = new Date();
        mallCommentDetail.setCommentTime(date);
        mallCommentDetailMapper.insert(mallCommentDetail);

        return ServerResponse.createBySuccess("回复成功");
    }

    @AuthRequired
    @DeleteMapping("deleteReply")
    @ApiOperation(value = "删除回复")
    public ServerResponse deleteReply(Integer comment_detail_id) {
        MallCommentDetail mallCommentDetail = mallCommentDetailMapper.selectByPrimaryKey(comment_detail_id);
        MallComment mallComment = mallCommentMapper.selectByPrimaryKey(mallCommentDetail.getCommentId());
        mallComment.setCommentNum(mallComment.getCommentNum() - 1);
        mallCommentMapper.updateByPrimaryKey(mallComment);

        Integer count = mallCommentDetailMapper.deleteByPrimaryKey(comment_detail_id);

        if (count != 0) {
            return ServerResponse.createBySuccess("删除成功");
        } else {
            return ServerResponse.createByErrorMessage("删除失败");
        }
    }

    @AuthRequired
    @PostMapping("addSampleOrder")
    @ApiOperation(value = "添加拿样单")
    public ServerResponse<String> addSampleOrder(String username, Integer product_id,
                                                 Integer shipping_id, BigDecimal quantity, String grade) {
        MallOrder mallOrder = new MallOrder();
        long currentTime = System.currentTimeMillis();
        Long orderNo = currentTime + new Random().nextInt(100);
        mallOrder.setOrderNo(orderNo);

        SysUser user = sysUserMapper.selectByUserName(username);
        mallOrder.setUserId(user.getUserId());
        mallOrder.setShippingId(shipping_id);
        mallOrder.setPayment(new BigDecimal(0));
        mallOrder.setPaymentType(1);
        mallOrder.setPostage(20);
        mallOrder.setStatus(10);

        Date date = new Date();
        mallOrder.setCreateTime(date);
        mallOrder.setDistcode(2);
        mallOrder.setEnabled(1);
        mallOrder.setIsSample(1);
        mallOrder.setShopId(1);
        mallOrder.setMembertoken("NULL");
        mallOrderMapper.insert(mallOrder);

        MallOrder mallOrder1 = mallOrderMapper.selectByOrderNo(orderNo);
        MallOrderItem mallOrderItem = new MallOrderItem();
        MallProduct mallProduct = mallProductMapper.selectByPrimaryKey(product_id);
        mallOrderItem.setUserId(user.getUserId());
        mallOrderItem.setOrderNo(orderNo);
        mallOrderItem.setProductId(product_id);
        mallOrderItem.setProductName(mallProduct.getName());
        mallOrderItem.setProductImage(mallProduct.getMainImage());
        mallOrderItem.setCurrentUnitPrice(new BigDecimal(0));
        mallOrderItem.setQuantity(quantity);
        mallOrderItem.setTotalPrice(new BigDecimal(0));
        mallOrderItem.setCreateTime(date);
        mallOrderItem.setCommentCheck(0);
        mallOrderItem.setGrade(grade);

        String[] str;
        str = mallProduct.getName().split(",");
        mallOrderItem.setProductModel(str[2]);
        mallOrderItemMapper.insert(mallOrderItem);

        return ServerResponse.createBySuccess("添加成功");
    }

    @AuthRequired
    @GetMapping("getVoucher")
    @ApiOperation(value = "获取定金单据")
    public ServerResponse<MallPaymentVoucher> getVoucher(Long order_no) {
        MallPaymentVoucher mallPaymentVoucher = mallPaymentVoucherMapper.selectByOrderNo(order_no);

        return ServerResponse.createBySuccess(mallPaymentVoucher);
    }

    @AuthRequired
    @GetMapping("getVoucher2")
    @ApiOperation(value = "获取分期单据")
    public ServerResponse<MallPaymentVoucher> getVoucher2(Integer voucher_id) {
        MallPaymentVoucher mallPaymentVoucher = mallPaymentVoucherMapper.selectByPrimaryKey(voucher_id);

        return ServerResponse.createBySuccess(mallPaymentVoucher);
    }

    @AuthRequired
    @GetMapping("getVoucherList")
    @ApiOperation(value = "获取分期凭证列表")
    public ServerResponse<PageInfo<MallPaymentVoucher>> getVoucherList(
            @RequestParam(value = "pageNum", defaultValue = "1")
                    int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "100")
                    int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<MallPaymentVoucher> voucherList = mallPaymentVoucherMapper.selectExceptDeposit();
        PageInfo<MallPaymentVoucher> pageResult = new PageInfo<>(voucherList);

        return ServerResponse.createBySuccess(pageResult);
    }

    @AuthRequired
    @PostMapping("editOrderPrice")
    @ApiOperation(value = "编辑订单价格")
    public ServerResponse<String> editOrderPrice(BigDecimal payment, Long order_no) {
        mallOrderMapper.updateOrderPrice(order_no, payment);

        return ServerResponse.createBySuccess("修改成功");
    }

    @AuthRequired
    @GetMapping("searchMyOrder")
    @ApiOperation(value = "搜索业务员订单")
    public ServerResponse<PageVo<MallOrderManageVo>> searchMyOrder(String order_no, Integer status,
                                                                   Integer user_id, String bus_name,
                                                                   @RequestParam(value = "pageNum", defaultValue = "1")
                                                                           int pageNum,
                                                                   @RequestParam(value = "pageSize", defaultValue = "100")
                                                                           int pageSize) {
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        List<MallOrderManageVo> orderManageVoList;
        total = mallOrderMapper.searchMyDealerOrderCount(order_no,
                status, user_id, bus_name);
        orderManageVoList = mallOrderMapper.searchMyDealerOrder(order_no,
                status, user_id, bus_name, pageStart, pageSize);

        for (MallOrderManageVo anOrderManageVoList : orderManageVoList) {
            MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(anOrderManageVoList.getId());
            List<MallOrderItemManageVo> mallOrderItemMappers = mallOrderItemMapper.selectByOrderNo(mallOrder.getOrderNo());
            anOrderManageVoList.setProductList(mallOrderItemMappers);
        }


        PageVo<MallOrderManageVo> pageVo = new PageVo<>(pageNum, pageSize, total, orderManageVoList);


        return ServerResponse.createBySuccess(pageVo);
    }

    @AuthRequired
    @GetMapping("searchSalesManSampleOrder")
    @ApiOperation(value = "搜索业务员拿样订单")
    public ServerResponse<PageInfo<MallOrderManageVo>> searchSalesManSampleOrder(String order_no, Integer status,
                                                                                 Integer user_id, String bus_name,
                                                                                 @RequestParam(value = "pageNum", defaultValue = "1")
                                                                                         int pageNum,
                                                                                 @RequestParam(value = "pageSize", defaultValue = "100")
                                                                                         int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<MallOrderManageVo> orderManageVoList;
        orderManageVoList = mallOrderMapper.searchSalesManSampleOrder(order_no,
                status, user_id, bus_name);

        for (MallOrderManageVo anOrderManageVoList : orderManageVoList) {
            MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(anOrderManageVoList.getId());
            List<MallOrderItemManageVo> mallOrderItemMappers = mallOrderItemMapper.selectByOrderNo(mallOrder.getOrderNo());
            anOrderManageVoList.setProductList(mallOrderItemMappers);
        }

        PageInfo<MallOrderManageVo> pageResult = new PageInfo<>(orderManageVoList);

        return ServerResponse.createBySuccess(pageResult);
    }

    @AuthRequired
    @GetMapping("SearchOrderList")
    @ApiOperation(value = "搜索订单")
    public ServerResponse<PageVo<MallOrderManageVo>> SearchOrderList(String order_no, Integer dept_id,
                                                                     Integer status, String bus_name,
                                                                     @RequestParam(value = "pageNum", defaultValue = "1")
                                                                             int pageNum,
                                                                     @RequestParam(value = "pageSize", defaultValue = "100")
                                                                             int pageSize) {
        Integer pageStart = pageSize * (pageNum - 1);

        Integer shop_id = mallShopMapper.selectByDeptId(dept_id);
        List<MallOrderManageVo> orderManageVoList;
        Integer total;
        if (shop_id == null) {
            total = mallOrderMapper.searchDealerOrderCount(order_no,
                    status, dept_id, bus_name);
            orderManageVoList = mallOrderMapper.searchDealerOrder(order_no,
                    status, dept_id, bus_name, pageStart, pageSize);
        } else {
            total = mallOrderMapper.searchTotalOrderCount(order_no,
                    status, dept_id, bus_name);
            orderManageVoList = mallOrderMapper.searchTotalOrder(order_no,
                    status, dept_id, bus_name, pageStart, pageSize);
        }

        System.out.println(orderManageVoList);

        for (MallOrderManageVo anOrderManageVoList : orderManageVoList) {
            MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(anOrderManageVoList.getId());
            List<MallOrderItemManageVo> mallOrderItemMappers = mallOrderItemMapper.selectByOrderNo(mallOrder.getOrderNo());
            anOrderManageVoList.setProductList(mallOrderItemMappers);
        }

        PageVo<MallOrderManageVo> pageVo = new PageVo<>(pageNum, pageSize, total, orderManageVoList);


        return ServerResponse.createBySuccess(pageVo);
    }

    @AuthRequired
    @PostMapping("sendOrderItem")
    @ApiOperation(value = "发货")
    public ServerResponse sendOrderItem(@RequestBody List<myItem> myForm, String fufillmentNo) {
        //判断发货编号是否重复
        MallOrderFufillmentParent mallOrderFufillmentParent2 = mallOrderFufillmentParentMapper.selectByNo(fufillmentNo);
        if (mallOrderFufillmentParent2 != null) {
            return ServerResponse.createByErrorMessage("发货编号已存在");
        }
        //判断发货列表是否为空
        Map<Integer, String> map = new HashMap<>();
        System.out.println(myForm);
        if (myForm.size() == 0) {
            return ServerResponse.createByErrorMessage("发货列表为空");
        }
        long currentTime = System.currentTimeMillis();


        for (myItem aMyForm : myForm) {
            Integer item_id = aMyForm.getItem_id();
            BigDecimal weight = aMyForm.getWeight();
            Integer order_id = mallOrderItemMapper.selectOrderId(item_id);
            Long order_no = mallOrderMapper.selectByPrimaryKey(order_id).getOrderNo();
            //设置默认评论
            List<FufillmentVo2> list = mallOrderFufillmentParentMapper.selectByOrderNo(order_no);
            if (list.size() == 0) {
                Map<String, String> map2 = new HashMap<>();
                map2.put("orderNo", order_no.toString());

                HttpClientUtil.doGetWithParam("http://47.98.169.251:9094/comment/createDefaultComment", map2);
            }

            //判断是否创建了mallOrderFufillmentParent
            if (map.containsKey(order_id)) {
                String fufillment_no = map.get(order_id);
                MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(order_id);
                MallOrderItem mallOrderItem = mallOrderItemMapper.selectByPrimaryKey(item_id);
                Integer customer_id = sysCustomerMapper.selectCustomerId(mallOrder.getUserId());

                //创建发货历史
                MallOrderFufillment mallOrderFufillment = new MallOrderFufillment();
                String str = mallOrderItem.getProductName();
                List<String> idList = Arrays.asList(str.split(","));
                mallOrderFufillment.setFufillmentNo(fufillment_no);
                mallOrderFufillment.setCustomerId(customer_id);
                mallOrderFufillment.setIsReceived(0);
                mallOrderFufillment.setModel(idList.get(2));
                mallOrderFufillment.setOrderItemId(item_id);
                mallOrderFufillment.setSendTime(new Date());
                mallOrderFufillment.setWeight(weight);
                mallOrderFufillmentMapper.insert(mallOrderFufillment);
                //更新订单产品余量
                mallOrderItem.setResidualQuantity(mallOrderItem.getResidualQuantity().subtract(weight));
                mallOrderItemMapper.updateByPrimaryKey(mallOrderItem);

                //修改订单状态
                mallOrderMapper.updateByOrderNo(60, mallOrder.getOrderNo());
//                Integer count = mallOrderItemMapper.checkResidualQuantity(mallOrder.getOrderNo());
//                if (count>0){
//                    mallOrderMapper.updateByOrderNo(60, mallOrder.getOrderNo());
//                }else {
//                    mallOrderMapper.updateByOrderNo(50, mallOrder.getOrderNo());
//                }

                //设置发货历史主表未支付金额
                MallOrderFufillmentParent mallOrderFufillmentParent = mallOrderFufillmentParentMapper.selectByNo(fufillment_no);
                mallOrderFufillmentParent.setUnpayment(mallOrderFufillmentParent.getUnpayment().add(mallOrderItem.getCurrentUnitPrice().multiply(weight)));
                mallOrderFufillmentParentMapper.updateByPrimaryKey(mallOrderFufillmentParent);

                SysCustomerCredit sysCustomerCredit = sysCustomerCreditMapper.selectByCustomerId(customer_id);
                if (sysCustomerCredit != null && sysCustomerCredit.getQuota() != 0 && sysCustomerCredit.getAccountPeriod() != 0) {
                    //添加客户欠款
                    MallPaymentUnpay mallPaymentUnpay = mallPaymentUnpayMapper.selectByCustomerId(customer_id);
                    method2(mallOrder, customer_id, mallPaymentUnpay);

                    //设置发货单数据
                    Calendar calendar = Calendar.getInstance();
                    SysCustomerStatement sysCustomerStatement = sysCustomerStatementMapper.selectByCustomerIdAndTime(customer_id, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1);
                    MallOrderItem mallOrderItem2 = mallOrderItemMapper.selectByPrimaryKey(item_id);
                    BigDecimal unpayment = mallOrderItem2.getCurrentUnitPrice().multiply(weight);
                    method3(customer_id, calendar, sysCustomerStatement, unpayment);
                }
            } else {
                //创建发货历史主表
                MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(order_id);

                MallOrderFufillmentParent mallOrderFufillmentParent = new MallOrderFufillmentParent();
                mallOrderFufillmentParent.setCreateTime(new Date());
                mallOrderFufillmentParent.setFufillmentNo(fufillmentNo);
                mallOrderFufillmentParent.setOrderNo(mallOrder.getOrderNo());
                mallOrderFufillmentParent.setIsReceived(0);
                mallOrderFufillmentParent.setAddress(aMyForm.getAddress());
                Integer customer_id = sysCustomerMapper.selectCustomerId(mallOrder.getUserId());
                SysCustomerCredit sysCustomerCredit = sysCustomerCreditMapper.selectByCustomerId(customer_id);
                Integer accountPeriod = 0;
                if (sysCustomerCredit != null) {
                    accountPeriod = sysCustomerCredit.getAccountPeriod();
                }
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_MONTH, accountPeriod);
                mallOrderFufillmentParent.setPayTime(calendar.getTime());
                mallOrderFufillmentParent.setStatus(0);
                MallOrderItem mallOrderItem = mallOrderItemMapper.selectByPrimaryKey(item_id);
                BigDecimal unpayment = mallOrderItem.getCurrentUnitPrice().multiply(weight);
                mallOrderFufillmentParent.setUnpayment(unpayment);
                mallOrderFufillmentParentMapper.insert(mallOrderFufillmentParent);

                //判断是否创建主表的条件插入
                map.put(order_id, fufillmentNo);

                MallOrderFufillment mallOrderFufillment = new MallOrderFufillment();
                String str = mallOrderItem.getProductName();
                List<String> idList = Arrays.asList(str.split(","));
                mallOrderFufillment.setFufillmentNo(fufillmentNo);
                mallOrderFufillment.setCustomerId(customer_id);
                mallOrderFufillment.setIsReceived(0);
                mallOrderFufillment.setModel(idList.get(2));
                mallOrderFufillment.setOrderItemId(item_id);
                mallOrderFufillment.setSendTime(new Date());
                mallOrderFufillment.setWeight(weight);
                mallOrderFufillmentMapper.insert(mallOrderFufillment);
                mallOrderItem.setResidualQuantity(mallOrderItem.getResidualQuantity().subtract(weight));
                mallOrderItemMapper.updateByPrimaryKey(mallOrderItem);


                //修改订单状态
                mallOrderMapper.updateByOrderNo(60, mallOrder.getOrderNo());
//                Integer count = mallOrderItemMapper.checkResidualQuantity(mallOrder.getOrderNo());
//                if (count>0){
//                    mallOrderMapper.updateByOrderNo(60, mallOrder.getOrderNo());
//                }else {
//                    mallOrderMapper.updateByOrderNo(50, mallOrder.getOrderNo());
//                }

                SysCustomerCredit sysCustomerCredit2 = sysCustomerCreditMapper.selectByCustomerId(customer_id);
                if (sysCustomerCredit2 != null && sysCustomerCredit2.getQuota() != 0 && sysCustomerCredit2.getAccountPeriod() != 0) {
                    //添加客户欠款
                    MallPaymentUnpay mallPaymentUnpay = mallPaymentUnpayMapper.selectByCustomerId(customer_id);
                    method2(mallOrder, customer_id, mallPaymentUnpay);

                    //设置发货单数据
                    Calendar calendar2 = Calendar.getInstance();
                    SysCustomerStatement sysCustomerStatement = sysCustomerStatementMapper.selectByCustomerIdAndTime(customer_id, calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH) + 1);
                    MallOrderItem mallOrderItem2 = mallOrderItemMapper.selectByPrimaryKey(item_id);
                    BigDecimal unpayment2 = mallOrderItem2.getCurrentUnitPrice().multiply(weight);
                    if (sysCustomerStatement != null) {
                        sysCustomerStatement.setThisMonthFufillmentPayment(sysCustomerStatement.getThisMonthFufillmentPayment().add(unpayment2));
                        sysCustomerStatement.setThisMonthUnpayment(sysCustomerStatement.getThisMonthUnpayment().add(unpayment2));
                        sysCustomerStatementMapper.updateByPrimaryKey(sysCustomerStatement);
                    } else {
                        SysCustomerStatement sysCustomerStatement3 = new SysCustomerStatement();
                        sysCustomerStatement3.setCustomerId(customer_id);
                        sysCustomerStatement3.setYear(calendar2.get(Calendar.YEAR));
                        sysCustomerStatement3.setMonth(calendar2.get(Calendar.MONTH) + 1);
                        SysCustomerStatement sysCustomerStatement2 = sysCustomerStatementMapper.selectByCustomerIdAndTime(customer_id, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH));
                        if (sysCustomerStatement2 == null) {
                            sysCustomerStatement3.setLastMonthUnpayment(new BigDecimal(0));
                            sysCustomerStatement3.setThisMonthUnpayment(new BigDecimal(0));
                        } else {
                            sysCustomerStatement3.setLastMonthUnpayment(sysCustomerStatement2.getThisMonthUnpayment());
                            sysCustomerStatement3.setThisMonthUnpayment(sysCustomerStatement2.getThisMonthUnpayment());
                        }
                        sysCustomerStatement3.setThisMonthReturnPayment(new BigDecimal(0));
                        sysCustomerStatement3.setThisMonthUnpayment(sysCustomerStatement3.getThisMonthUnpayment().add(unpayment2));
                        sysCustomerStatement3.setThisMonthFufillmentPayment(unpayment2);
                        sysCustomerStatement3.setCreateTime(new Date());
                        sysCustomerStatementMapper.insert(sysCustomerStatement3);
                    }
                }
            }
        }

        return ServerResponse.createBySuccess("发货成功");
    }

    @GetMapping("getOrderFufillment")
    @ApiOperation(value = "获得发货历史列表")
    public ServerResponse<List<FufillmentVo>> getOrderFufillment(String token, Integer user_id, Integer customer_id, String start_time, String end_time) throws Exception {
        if (token != null && !token.equals("")) {
            SysUser user;
            Jedis jedis = jedisPool.getResource();
            user = tokenUtil.tokenToUser(jedis, token).getData();
            jedis.close();
            user_id = user.getUserId();
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date StratTime = null;
        Date EndTime = null;
        if (start_time != null && !start_time.equals("") && end_time != null && !end_time.equals("")) {
            StratTime = sdf.parse(start_time);
            EndTime = sdf.parse(end_time);
        }
        System.out.println(StratTime);
        System.out.println(EndTime);
        List<FufillmentVo> list = mallOrderFufillmentMapper.selectMyFufillment(customer_id, user_id, StratTime, EndTime);
        System.out.println(list);
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @GetMapping("getMyCustomer")
    @ApiOperation(value = "获得我的客户")
    public ServerResponse<List<MyCustomerVo>> getMyCustomer(Integer user_id) {
        List<MyCustomerVo> list = sysCustomerMapper.selectMyCustomer(user_id);
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @GetMapping("getDealerCustomer")
    @ApiOperation(value = "获得办事处客户")
    public ServerResponse<List<MyCustomerVo>> getDealerCustomer(Integer dept_id) {
        Integer dealer_id = mallDealerMapper.selectDealerId(dept_id);
        List<MyCustomerVo> list = sysCustomerMapper.selectDealerCustomer(dealer_id);
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @GetMapping("getCustomer")
    @ApiOperation(value = "获得客户")
    public ServerResponse<List<MyCustomerVo>> getCustomer() {
        List<MyCustomerVo> list = sysCustomerMapper.selectCustomer();
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @GetMapping("getDealerOrderFufillment")
    @ApiOperation(value = "获得办事处发货历史列表")
    public ServerResponse<List<FufillmentVo>> getDealerOrderFufillment(Integer dept_id, Integer customer_id, String start_time, String end_time) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date StratTime = null;
        Date EndTime = null;
        if (start_time != null && !start_time.equals("") && end_time != null && !end_time.equals("")) {
            StratTime = sdf.parse(start_time);
            EndTime = sdf.parse(end_time);
        }
        System.out.println(StratTime);
        System.out.println(EndTime);
        Integer dealer_id = mallDealerMapper.selectDealerId(dept_id);
        List<FufillmentVo> list = mallOrderFufillmentMapper.selectDealerFufillment(customer_id, dealer_id, StratTime, EndTime);
        System.out.println(list);
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @PostMapping("createPlanOrder")
    @ApiOperation(value = "创建计划订单")
    public ServerResponse<String> createPlanOrder(@RequestBody List<planOrderVo> orderItemList, Integer customerUserId, Integer shippingId, Integer distCode) {
        MallOrder mallOrder = new MallOrder();
        Integer dept_id = sysUserDeptMapper.selectByUserId(customerUserId).getDeptId();
        Integer customer_id = sysCustomerMapper.selectCustomerId(customerUserId);
        Integer dealer_id = sysCustomerMapper.selectByPrimaryKey(customer_id).getDealerId();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
        Long order_no = NumberUtil.createOrderNo(dealer_id);
        BigDecimal payment = new BigDecimal(0);
        for (planOrderVo anOrderItemList : orderItemList) {
            payment.add(anOrderItemList.getPrice());
            MallOrderItem mallOrderItem = new MallOrderItem();
            mallOrderItem.setUserId(customerUserId);
            mallOrderItem.setOrderNo(order_no);
            MallProduct mallProduct = mallProductMapper.selectByProductId(anOrderItemList.getProduct_id());
            mallOrderItem.setProductId(mallProduct.getId());
            mallOrderItem.setProductName(mallProduct.getName());
            String str2 = mallProduct.getMainImage();
            String[] image = str2.split(",");
            mallOrderItem.setProductImage(image[0]);
            mallOrderItem.setCurrentUnitPrice(anOrderItemList.getPrice());
            mallOrderItem.setQuantity(anOrderItemList.getWeight());
            mallOrderItem.setTotalPrice(anOrderItemList.getPrice().multiply(anOrderItemList.getWeight()));
            mallOrderItem.setCreateTime(new Date());
            mallOrderItem.setGrade(anOrderItemList.getGrade());
            mallOrderItem.setCommentCheck(0);
            String str = mallProduct.getName();
            String[] name = str.split(",");
            mallOrderItem.setProductModel(name[2]);
            mallOrderItem.setResidualQuantity(anOrderItemList.getWeight());
            mallOrderItemMapper.insert(mallOrderItem);
            System.out.println(anOrderItemList.getPlan_item_id());
            Integer planItemId = anOrderItemList.getPlan_item_id();
            MallProductPlanItem mallProductPlanItem = mallProductPlanItemMapper.selectByPrimaryKey(planItemId);
            mallProductPlanItem.setResidualQuantity(mallProductPlanItem.getResidualQuantity().subtract(anOrderItemList.getWeight()));
            mallProductPlanItemMapper.updateByPrimaryKey(mallProductPlanItem);
        }
        Integer status = 0;
//        SysCustomerCredit sysCustomerCredit = sysCustomerCreditMapper.selectByCustomerId(customer_id);
//        if (sysCustomerCredit == null) {
//            status = 30;
//        }else {
//            if ((sysCustomerCredit.getQuota()==0&&sysCustomerCredit.getAccountPeriod()==0)){
//                status = 30;
//            }else {
//                status = 20;
//            }
//        }

        mallOrder.setUserId(customerUserId);
        mallOrder.setStatus(10);
        mallOrder.setDistcode(distCode);
        mallOrder.setShopId(1);
        mallOrder.setExamCheck(0);
        mallOrder.setIsSample(0);
        mallOrder.setDealerId(dealer_id);
        mallOrder.setCreateTime(new Date());
        mallOrder.setEnabled(1);
        mallOrder.setPaymentType(1);
        mallOrder.setShippingId(shippingId);
        mallOrder.setOrderNo(order_no);
        mallOrder.setPayment(payment);
        mallOrder.setSubmitPerson(2);
        mallOrder.setPlanCheck(1);
        mallOrder.setStatus1(1);
        mallOrder.setStatus2(0);
        mallOrder.setStatus3(0);


        mallOrderMapper.insert(mallOrder);


        return ServerResponse.createBySuccess("创建成功");
    }

    @AuthRequired
    @GetMapping("getFufillmentByOrderNo")
    @ApiOperation(value = "获得订单发货历史")
    public ServerResponse<List<FufillmentVo2>> getFufillmentByOrderNo(Long order_no) {
        List<FufillmentVo2> list = mallOrderFufillmentParentMapper.selectByOrderNo(order_no);
        for (FufillmentVo2 aList : list) {
            List<FufillmentVo> list1 = mallOrderFufillmentMapper.selectByFulillmentNo(aList.getFufillment_no());
            aList.setFufillmentVoList(list1);
        }
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @GetMapping("getVoucherListByUsername")
    @ApiOperation(value = "根据用户名得到支付列表")
    public ServerResponse getVoucherListByUsername(String username) {
        SysUser sysUser = sysUserMapper.selectByUserName(username);
        if (sysUser == null) {
            return ServerResponse.createByErrorMessage("该用户不存在");
        }
        Integer user_id = sysUser.getUserId();
        List<MallPaymentVoucher> list = mallPaymentVoucherMapper.selectByUserId(user_id);
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @GetMapping("getVoucherListByCustomerId")
    @ApiOperation(value = "根据公司id得到支付列表")
    public ServerResponse<List<MallPaymentVoucherVo>> getVoucherListByCustomerId(Integer customer_id, Integer month, Integer year) {
        Date startTime = null;
        Date endTime = null;
        System.out.println(month);
        System.out.println(year);
        if (year != null && month != null) {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 1);

            final int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
            cal.set(Calendar.DAY_OF_MONTH, start);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);

            startTime = cal.getTime();
            final int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            cal.set(Calendar.DAY_OF_MONTH, end);
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
            endTime = cal.getTime();
        }
        System.out.println(startTime);
        System.out.println(endTime);
        List<MallPaymentVoucherVo> list = mallPaymentVoucherMapper.selectByCustomerId(customer_id, startTime, endTime);
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @PostMapping("saveVoucher")
    @ApiOperation(value = "支付凭证审核通过")
    public ServerResponse saveVoucher(Integer voucherId, String opinion) {
        MallPaymentVoucher mallPaymentVoucher = mallPaymentVoucherMapper.selectByPrimaryKey(voucherId);
        if (mallPaymentVoucher == null) {
            return ServerResponse.createByErrorMessage("交易凭证不存在");
        }
        mallPaymentVoucher.setIsAdopt(1);
        mallPaymentVoucher.setIsPay(1);
        mallPaymentVoucher.setOpinion(opinion);

        if (mallPaymentVoucher.getVoucherImg() == null) {
            return ServerResponse.createByErrorMessage("未上传支付凭证");
        }
        //更新支付凭证状态
        mallPaymentVoucherMapper.updateByPrimaryKey(mallPaymentVoucher);

        Integer user_id = mallPaymentVoucher.getUserId();
        Integer customer_id = sysCustomerMapper.selectCustomerId(user_id);
        //更新客户未支付金额
        MallPaymentUnpay mallPaymentUnpay = mallPaymentUnpayMapper.selectByCustomerId(customer_id);
        if (mallPaymentUnpay == null) {
            MallPaymentUnpay mallPaymentUnpay1 = new MallPaymentUnpay();
            mallPaymentUnpay1.setCustomerId(customer_id);
            mallPaymentUnpay1.setUnpayPayment(new BigDecimal(0).subtract(mallPaymentVoucher.getFactPayment()));
            mallPaymentUnpayMapper.insert(mallPaymentUnpay1);
        } else {
            mallPaymentUnpay.setUnpayPayment(mallPaymentUnpay.getUnpayPayment().subtract(mallPaymentVoucher.getFactPayment()));
            mallPaymentUnpayMapper.updateByPrimaryKey(mallPaymentUnpay);
        }
        //修改发货历史未支付金额和状态
        List<MallOrderFufillmentParent> list = mallOrderFufillmentParentMapper.selectByCustomerId(customer_id);
        BigDecimal fact_payment = mallPaymentVoucher.getFactPayment();
        for (MallOrderFufillmentParent aList : list) {
            if (fact_payment.compareTo(new BigDecimal(0)) > 0) {
                if (fact_payment.compareTo(aList.getUnpayment()) >= 0) {
                    fact_payment = fact_payment.subtract(aList.getUnpayment());
                    aList.setStatus(1);
                    aList.setUnpayment(new BigDecimal(0));
                    mallOrderFufillmentParentMapper.updateByPrimaryKey(aList);
                } else {
                    aList.setUnpayment(aList.getUnpayment().subtract(fact_payment));
                    mallOrderFufillmentParentMapper.updateByPrimaryKey(aList);
                    fact_payment = new BigDecimal(0);
                }
            } else {
                break;
            }
        }

        //设置发货单数据
        Calendar calendar = Calendar.getInstance();
        SysCustomerStatement sysCustomerStatement = sysCustomerStatementMapper.selectByCustomerIdAndTime(customer_id, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1);
        if (sysCustomerStatement != null) {
            sysCustomerStatement.setThisMonthReturnPayment(sysCustomerStatement.getThisMonthReturnPayment().add(mallPaymentVoucher.getFactPayment()));
            sysCustomerStatement.setThisMonthUnpayment(sysCustomerStatement.getThisMonthUnpayment().subtract(mallPaymentVoucher.getFactPayment()));
            sysCustomerStatementMapper.updateByPrimaryKey(sysCustomerStatement);
        } else {
            SysCustomerStatement sysCustomerStatement3 = new SysCustomerStatement();
            sysCustomerStatement3.setCustomerId(customer_id);
            sysCustomerStatement3.setYear(calendar.get(Calendar.YEAR));
            sysCustomerStatement3.setMonth(calendar.get(Calendar.MONTH) + 1);
            SysCustomerStatement sysCustomerStatement2 = sysCustomerStatementMapper.selectByCustomerIdAndTime(customer_id, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH));
            if (sysCustomerStatement2 == null) {
                sysCustomerStatement3.setLastMonthUnpayment(new BigDecimal(0));
            } else {
                sysCustomerStatement3.setLastMonthUnpayment(sysCustomerStatement2.getThisMonthUnpayment());
            }
            sysCustomerStatement3.setThisMonthUnpayment(new BigDecimal(0).subtract(mallPaymentVoucher.getFactPayment()));
            sysCustomerStatement3.setThisMonthFufillmentPayment(new BigDecimal(0));
            sysCustomerStatement3.setThisMonthReturnPayment(mallPaymentVoucher.getFactPayment());
            sysCustomerStatement3.setCreateTime(new Date());
            sysCustomerStatementMapper.insert(sysCustomerStatement3);
        }

        return ServerResponse.createBySuccess("审核通过");
    }

    @AuthRequired
    @PostMapping("unpassVoucher")
    @ApiOperation(value = "支付凭证审核不通过")
    public ServerResponse unpassVoucher(Integer voucherId, String opinion) {
        MallPaymentVoucher mallPaymentVoucher = mallPaymentVoucherMapper.selectByPrimaryKey(voucherId);
        if (mallPaymentVoucher == null) {
            return ServerResponse.createByErrorMessage("交易凭证不存在");
        }
        mallPaymentVoucher.setIsAdopt(2);
        mallPaymentVoucher.setIsPay(0);
        mallPaymentVoucher.setOpinion(opinion);

        mallPaymentVoucherMapper.updateByPrimaryKey(mallPaymentVoucher);

        return ServerResponse.createBySuccess("审核不通过");
    }

    @AuthRequired
    @GetMapping("getFufillmentByCustomerId")
    @ApiOperation(value = "根据客户id获得发货历史")
    public ServerResponse<List<FufillmentVo2>> getFufillmentByCustomerId(Integer customer_id) {
        List<FufillmentVo2> list = mallOrderFufillmentParentMapper.selectByCustomer(customer_id);
        for (FufillmentVo2 aList : list) {
            List<FufillmentVo> list1 = mallOrderFufillmentMapper.selectByFulillmentNo(aList.getFufillment_no());
            aList.setFufillmentVoList(list1);
        }
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @GetMapping("getOrderDetailMessage")
    @ApiOperation(value = "订单详情提示信息")
    public ServerResponse getOrderDetailMessage(Integer customerId) {
        SysCustomer sysCustomer = sysCustomerMapper.selectByPrimaryKey(customerId);
        if (sysCustomer == null) {
            return ServerResponse.createByErrorMessage("无该客户");
        }
        SysCustomerCredit sysCustomerCredit = sysCustomerCreditMapper.selectByCustomerId(customerId);
        if (sysCustomerCredit == null || (sysCustomerCredit.getQuota() == 0 && sysCustomerCredit.getAccountPeriod() == 0)) {
            List<MallOrder> list = mallOrderMapper.selectPassOrderByCustomerId(customerId);
            if (list.size() > 0) {
                Integer count1 = mallPaymentVoucherMapper.selectUnExamVoucher(customerId);
                if (count1 > 0) {
                    for (MallOrder aList : list) {
                        Long orderNo = aList.getOrderNo();
                        MallPaymentVoucher mallPaymentVoucher = mallPaymentVoucherMapper.selectByOrderNo(orderNo);
                        if (mallPaymentVoucher == null) {
                            return ServerResponse.createBySuccess("该客户有凭证未审核,审核通过订单未上传支付凭证");
                        }
                    }
                    return ServerResponse.createBySuccess("该客户有凭证未审核");
                } else {
                    for (MallOrder aList : list) {
                        Long orderNo = aList.getOrderNo();
                        MallPaymentVoucher mallPaymentVoucher = mallPaymentVoucherMapper.selectByOrderNo(orderNo);
                        if (mallPaymentVoucher == null) {
                            return ServerResponse.createBySuccess("该客户有审核通过订单未上传支付凭证");
                        }
                    }
                }
                return ServerResponse.createByErrorMessage("该现金客户一切正常");
            } else {
                return ServerResponse.createByErrorMessage("该客户没有待发货或凭证审核中的订单");
            }
        } else {
            return ServerResponse.createByErrorMessage("该用户为账期客户");
        }
    }

    @AuthRequired
    @GetMapping("getOrderDetail")
    @ApiOperation(value = "获得订单详情")
    public ServerResponse<List<MallOrderItemManageVo>> getOrderDetail(Long order_no) {
        List<MallOrderItemManageVo> list = mallOrderItemMapper.selectByOrderNo(order_no);
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @GetMapping("examOrder")
    @ApiOperation(value = "复审订单")
    public ServerResponse examOrder(Long order_no, Integer status, Integer role, String opinion, Integer user_id, String send_date) {
        MallOrderRemark mallOrderRemark = new MallOrderRemark();
        mallOrderRemark.setContent(opinion);
        mallOrderRemark.setOrderNo(order_no);
        mallOrderRemark.setUserId(user_id);
        mallOrderRemark.setCreateTime(new Date());
        MallOrder mallOrder = mallOrderMapper.selectByOrderNo(order_no);
        if (mallOrder == null) {
            return ServerResponse.createByErrorMessage("订单不存在");
        }
        if (status == 1) {
            //审核通过
            if (role == 0) {
                //财务审核
                mallOrder.setStatus3(1);
                mallOrderMapper.updateByPrimaryKey(mallOrder);
                mallOrderRemark.setType(3);
                mallOrderRemarkMapper.insert(mallOrderRemark);
            } else {
                //跟单员审核
                mallOrder.setStatus2(1);
                mallOrder.setStatus(20);
                mallOrder.setSendDate(send_date);
                mallOrderMapper.updateByPrimaryKey(mallOrder);
                mallOrderRemark.setType(2);
                mallOrderRemarkMapper.insert(mallOrderRemark);
                Integer customer_id = sysCustomerMapper.selectCustomerId(mallOrder.getUserId());

                SysCustomerCredit sysCustomerCredit2 = sysCustomerCreditMapper.selectByCustomerId(customer_id);
                method1(mallOrder, customer_id, sysCustomerCredit2);
            }
        } else {
            //审核不通过
            if (role == 0) {
                //财务审核
                mallOrder.setStatus1(3);
                mallOrder.setStatus2(0);
                mallOrder.setStatus3(2);
                mallOrderMapper.updateByPrimaryKey(mallOrder);
                mallOrderRemark.setType(3);
                mallOrderRemarkMapper.insert(mallOrderRemark);
            } else {
                //跟单员审核
                mallOrder.setStatus1(3);
                mallOrder.setStatus2(2);
                mallOrder.setStatus3(0);
                mallOrderMapper.updateByPrimaryKey(mallOrder);
                mallOrderRemark.setType(2);
                mallOrderRemarkMapper.insert(mallOrderRemark);
            }
        }
        return ServerResponse.createBySuccess("审核成功");
    }

    @GetMapping("getInsertOrder")
    @ApiOperation(value = "获取要插入的订selectInsertOrder单")
    public ServerResponse getInsertOrder() {
        MallOrder mallOrder = mallOrderMapper.selectInsertOrder();
        if (mallOrder == null) {
            return ServerResponse.createByErrorMessage("无订单需插入");
        }
        List<InsertVo> result = new ArrayList<>();
        List<MallOrderItemManageVo> list = mallOrderItemMapper.selectByOrderNo(mallOrder.getOrderNo());
        Integer customer_id = sysCustomerMapper.selectCustomerId(mallOrder.getUserId());
        SysCustomer sysCustomer = sysCustomerMapper.selectByPrimaryKey(customer_id);
        SysCustomerDetail sysCustomerDetail = sysCustomerDetailMapper.selectByBusName(sysCustomer.getBusName());
        if (sysCustomerDetail == null) {
            return ServerResponse.createByErrorMessage("客户不存在");
        }
        for (int i = 0; i < list.size(); i++) {
            List<MallOrderItemDetail> list1 = mallOrderItemDetailMapper.selectByItemId(list.get(i).getItemId());
            if (list1.size() == 0 && i == list.size() - 1) {
                mallOrder.setInsertStatus(1);
                mallOrderMapper.updateByPrimaryKey(mallOrder);
                return ServerResponse.createByErrorMessage("进入下个订单");
            }
            if (list1.size() != 0) {
                List<MallOrderItemDetail> list2 = mallOrderItemDetailMapper.selectByGroup(list1.get(0).getGroupNo());
                for (MallOrderItemDetail mallOrderItemDetail : list2) {
                    InsertVo insertVo = new InsertVo();
                    MallOrderRemark mallOrderRemark = mallOrderRemarkMapper.selectByOrderNoAndType(mallOrderItemDetail.getGroupNo(), 1);
                    String remark = "";
                    if (mallOrderRemark != null) {
                        String remark1 = mallOrderRemark.getContent();
                        String remark2 = dictionMapper.selectByKeyAndValue("payment_methods", mallOrderItemDetail.getPaymentMethods());
                        MallShipping mallShipping = mallShippingMapper.selectByShippingId(mallOrder.getShippingId());
                        remark = remark2 + "款到发货 " + remark1 + " " + mallShipping.getReceiverName() + " " + mallShipping.getReceiverPhone() + " " + mallShipping.getReceiverProvince() + mallShipping.getReceiverCity() + mallShipping.getReceiverDistrict() + mallShipping.getReceiverAddress();
                    }
                    insertVo.setBus_name(sysCustomerDetail.getCustomerCode());
                    insertVo.setRemark(remark);
                    insertVo.setMaterial_code(mallOrderItemDetail.getMaterialCode());
                    insertVo.setId(mallOrder.getId());
                    insertVo.setSales_organization(mallOrderItemDetail.getSalesOrganization());
                    insertVo.setSales_type(mallOrderItemDetail.getSalesType());
                    insertVo.setFreight_payment_type(mallOrderItemDetail.getFreightPaymentType());
                    insertVo.setPayment_methods(mallOrderItemDetail.getPaymentMethods());
                    insertVo.setRate(mallOrderItemDetail.getRate());
                    insertVo.setEx_factory_price(mallOrderItemDetail.getExFactoryPrice());
                    insertVo.setQuantity((mallOrderItemDetail.getQuantity()));
                    insertVo.setCheck_quantity((mallOrderItemDetail.getCheckQuantity()));
                    insertVo.setCurrentUnitPrice((mallOrderItemDetail.getCurrentUnitPrice()));
                    Integer salesman_id = sysCustomerMapper.selectSalesManId(mallOrder.getUserId());
                    insertVo.setUser_id(salesman_id);
                    insertVo.setGroup_no(mallOrderItemDetail.getGroupNo());
                    insertVo.setOrder_no(mallOrder.getOrderNo());
                    mallOrderItemDetail.setInsertStatus(1);
                    mallOrderItemDetailMapper.updateByPrimaryKey(mallOrderItemDetail);
                    result.add(insertVo);
                }
                return ServerResponse.createBySuccess(result);
            }
        }
        return ServerResponse.createByErrorMessage("未知错误");
    }

    @GetMapping("changeInsertOrder")
    @ApiOperation(value = "修改插入的订单的状态")
    public void changeInsertOrder(Integer id) {
        MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(id);
        mallOrder.setInsertStatus(1);
        mallOrderMapper.updateByPrimaryKey(mallOrder);
    }

    @AuthRequired
    @GetMapping("test")
    @ApiOperation(value = "测试视图")
    public ServerResponse<List<stock_price>> test() {
        List<stock_price> list = mallProductForwardStockMapper.test();
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @GetMapping("getOneOrderDetail")
    @ApiOperation(value = "获得订单详情")
    public ServerResponse<MallOrder> getOneOrderDetail(Long order_no) {
        MallOrder mallOrder = mallOrderMapper.selectByOrderNo(order_no);
        return ServerResponse.createBySuccess(mallOrder);
    }

    @AuthRequired
    @GetMapping("getMaterialCodeList")
    @ApiOperation(value = "获得物料编码列表")
    public ServerResponse<List<mallOrderItemDetailVo>> getMaterialCodeList(Integer id) {
        List<mallOrderItemDetailVo> list = mallOrderItemDetailMapper.selectByOrderId(id);
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @GetMapping("getGroupMaterialCodeList")
    @ApiOperation(value = "获得分组物料编码列表")
    public ServerResponse<List<MallOrderItemDetail>> getGroupMaterialCodeList(Long group) {
        List<MallOrderItemDetail> list = mallOrderItemDetailMapper.selectByGroup(group);
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @GetMapping("getGroupList")
    @ApiOperation(value = "获得分组列表")
    public ServerResponse getGroupList() {
        return ServerResponse.createBySuccess();
    }

    @AuthRequired
    @PostMapping("groupMaterialCode")
    @ApiOperation(value = "分组物料编码")
    public ServerResponse<String> groupMaterialCode(Integer[] orderItemDetailId, String salesOrganization, String salesType, String freightPaymentType, String paymentMethods, String opinion, Integer user_id) {
        Integer orderItemId = mallOrderItemDetailMapper.selectByPrimaryKey(orderItemDetailId[0]).getOrderItemId();
        Long order_no = mallOrderItemMapper.selectByPrimaryKey(orderItemId).getOrderNo();
        Integer dealer_id = mallOrderMapper.selectByOrderNo(order_no).getDealerId();
        Long group_no = NumberUtil.createGroupNo(dealer_id);
        for (Integer anOrderItemDetailId : orderItemDetailId) {
            MallOrderItemDetail mallOrderItemDetail = mallOrderItemDetailMapper.selectByPrimaryKey(anOrderItemDetailId);
            mallOrderItemDetail.setGroupNo(group_no);
            mallOrderItemDetail.setSalesOrganization(salesOrganization);
            mallOrderItemDetail.setSalesType(salesType);
            mallOrderItemDetail.setFreightPaymentType(freightPaymentType);
            mallOrderItemDetail.setPaymentMethods(paymentMethods);
            mallOrderItemDetailMapper.updateByPrimaryKey(mallOrderItemDetail);
        }
        MallOrderRemark mallOrderRemark = new MallOrderRemark();
        mallOrderRemark.setContent(opinion);
        mallOrderRemark.setOrderNo(group_no);
        mallOrderRemark.setUserId(user_id);
        mallOrderRemark.setType(1);
        mallOrderRemark.setCreateTime(new Date());
        mallOrderRemarkMapper.insert(mallOrderRemark);
        return ServerResponse.createBySuccess("分组成功");
    }

    @AuthRequired
    @GetMapping("getBiao1")
    @ApiOperation(value = "获得图表1数据")
    public ServerResponse<Map<String, java.io.Serializable>> getBiao1(Integer user_id, Integer year, Integer month) {
        final Calendar cal = Calendar.getInstance();
        if (year != null && month != null) {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 1);
        }
        final int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, start);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        Date startTime = cal.getTime();
        final int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, end);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);

        Date endTime = cal.getTime();
        Integer dealer_id = sysUserMapper.selectDealerIdByUserId(user_id);

        List<SysCustomer> customerList = sysCustomerMapper.selectXBiao1(dealer_id, startTime, endTime);
        String[] x = new String[customerList.size()];
        BigDecimal[] y = new BigDecimal[customerList.size()];
        for (int i = 0; i < customerList.size(); i++) {
            x[i] = customerList.get(i).getBusName();
            List<MallOrder> mallOrderList = mallOrderMapper.selectMonthPassOrderByCustomerId(customerList.get(i).getCustomerId(), startTime, endTime);
            BigDecimal payment = new BigDecimal(0);
            for (MallOrder mallOrder : mallOrderList) {
                payment = payment.add(mallOrder.getPayment());
            }
            y[i] = payment;
        }

        Map<String, java.io.Serializable> map = new HashMap<>();
        map.put("x", x);
        map.put("y", y);
        return ServerResponse.createBySuccess(map);
    }

    @AuthRequired
    @GetMapping("getBiao2")
    @ApiOperation(value = "获得图表2数据")
    public ServerResponse<Map<String, java.io.Serializable>> getBiao2(Integer user_id, Integer year, Integer month) {
        final Calendar cal = Calendar.getInstance();
        if (year != null && month != null) {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 1);
        }
        final int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, start);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        Date startTime = cal.getTime();
        final int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, end);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);

        Date endTime = cal.getTime();
        Integer dealer_id = sysUserMapper.selectDealerIdByUserId(user_id);

        List<SysCustomer> customerList = sysCustomerMapper.selectXBiao1(dealer_id, startTime, endTime);
        String[] x = new String[customerList.size()];
        BigDecimal[] y = new BigDecimal[customerList.size()];
        for (int i = 0; i < customerList.size(); i++) {
            x[i] = customerList.get(i).getBusName();
            List<MallOrder> mallOrderList = mallOrderMapper.selectMonthPassOrderByCustomerId(customerList.get(i).getCustomerId(), startTime, endTime);
            BigDecimal payment = new BigDecimal(0);
            for (MallOrder mallOrder : mallOrderList) {
                payment = payment.add(mallOrder.getPayment());
            }
            y[i] = payment.divide(new BigDecimal(mallOrderList.size()), 8, ROUND_HALF_DOWN);
        }

        Map<String, java.io.Serializable> map = new HashMap<>();
        map.put("x", x);
        map.put("y", y);
        return ServerResponse.createBySuccess(map);
    }

    @AuthRequired
    @GetMapping("getBiao3")
    @ApiOperation(value = "获得图表3数据")
    public ServerResponse<Map<String, java.io.Serializable>> getBiao3(Integer user_id, Integer year, Integer month) {
        String[] x = new String[31];
        BigDecimal[] y = new BigDecimal[31];
        Integer[] z = new Integer[31];
        int index = 0;
        final Calendar cal = Calendar.getInstance();
        Integer dealer_id = sysUserMapper.selectDealerIdByUserId(user_id);
        if (year != null && month != null) {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 1);
        }
        final int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, start);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        Date startTime;
        final int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, end);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);

        Date endTime;

        for (int i = 1; i <= end; i++) {
            cal.set(Calendar.DAY_OF_MONTH, i);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);

            startTime = cal.getTime();

            cal.set(Calendar.DAY_OF_MONTH, i);
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);

            endTime = cal.getTime();

            List<MallOrder> orderList = mallOrderMapper.selectByDay(dealer_id, startTime, endTime);

            BigDecimal total = new BigDecimal(0);

            for (MallOrder anOrderList : orderList) {
                total = total.add(anOrderList.getPayment());
            }

            if (total.compareTo(new BigDecimal(0)) != 0) {
                x[index] = month + "月" + i + "日";
                y[index] = total;
                z[index] = orderList.size();
                index = index + 1;
            }
        }

        String[] x1 = new String[index];
        BigDecimal[] y1 = new BigDecimal[index];
        Integer[] z1 = new Integer[index];

        for (int i = 0; i < index; i++) {
            x1[i] = x[i];
            y1[i] = y[i];
            z1[i] = z[i];
        }


        Map<String, java.io.Serializable> map = new HashMap<>();
        map.put("x", x1);
        map.put("y", y1);
        map.put("z", z1);
        return ServerResponse.createBySuccess(map);
    }

    @AuthRequired
    @GetMapping("getBiao4")
    @ApiOperation(value = "获得图表4数据")
    public ServerResponse<List<DataVo>> getBiao4(Integer user_id, Integer year, Integer month) {
        final Calendar cal = Calendar.getInstance();
        if (year != null && month != null) {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 1);
        }
        final int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, start);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        Date startTime = cal.getTime();
        final int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, end);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);

        Date endTime = cal.getTime();
        Integer dealer_id = sysUserMapper.selectDealerIdByUserId(user_id);

        List<DataVo> dataVos = mallOrderMapper.selectMonthProduct(dealer_id, startTime, endTime);

        return ServerResponse.createBySuccess(dataVos);
    }

    @AuthRequired
    @GetMapping("getBiao5")
    @ApiOperation(value = "获得图表5数据")
    public ServerResponse<List<DataVo>> getBiao5(Integer user_id, Integer year, Integer month) {
        final Calendar cal = Calendar.getInstance();
        if (year != null && month != null) {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 1);
        }
        final int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, start);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        Date startTime = cal.getTime();
        final int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, end);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);

        Date endTime = cal.getTime();
        Integer dealer_id = sysUserMapper.selectDealerIdByUserId(user_id);

        List<DataVo> dataVos = mallOrderMapper.selectMonthOrderArea(dealer_id, startTime, endTime);

        return ServerResponse.createBySuccess(dataVos);
    }

    @AuthRequired
    @GetMapping("getTotalBiao")
    @ApiOperation(value = "获取运营总表")
    public ServerResponse<List<Object>> getTotalBiao(String start_time, String end_time) throws Exception {
        //时间转换
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date StartTime = null;
        Date EndTime = null;
        if (start_time != null && !start_time.equals("") && end_time != null && !end_time.equals("")) {
            StartTime = sdf.parse(start_time);
            EndTime = sdf.parse(end_time);
        }

//        List<MallOrder> orderList = mallOrderMapper.selectOrderByTime(StartTime,EndTime);

        Calendar calendar = Calendar.getInstance();
        assert StartTime != null;
        calendar.setTime(StartTime);

        Long startTime = calendar.getTimeInMillis();
        assert EndTime != null;
        calendar.setTime(EndTime);
        Long endTime = calendar.getTimeInMillis();
        Long oneDay = 1000 * 60 * 60 * 24L;

        Long time = startTime;

        List<String> timeList = new ArrayList<>();
        List<BigDecimal> totalSumList = new ArrayList<>();
        List<BigDecimal> totalPriceList = new ArrayList<>();
        List<BigDecimal> unitPriceList = new ArrayList<>();
        List<Integer> totalCountList = new ArrayList<>();
        while (time <= endTime) {
            Date d = new Date(time);
            calendar.setTime(d);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            Date oneDayStart = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
            Date oneDayEnd = calendar.getTime();
            List<MallOrder> orderList = mallOrderMapper.selectOrderByTime(oneDayStart, oneDayEnd);
            if (orderList.size() != 0) {
                timeList.add(calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日");
                BigDecimal totalSum = mallOrderMapper.selectTotalSum(oneDayStart, oneDayEnd).divide(new BigDecimal(1000), 2, ROUND_HALF_DOWN);
                totalSumList.add(totalSum);
                BigDecimal totalPrice = mallOrderMapper.selectTotalPrice(oneDayStart, oneDayEnd).divide(new BigDecimal(10000), 2, ROUND_HALF_DOWN);
                totalPriceList.add(totalPrice);
                BigDecimal unitPrice = totalPrice.divide(new BigDecimal(orderList.size()), 2, ROUND_HALF_DOWN);
                unitPriceList.add(unitPrice);
                totalCountList.add(orderList.size());
            } else {
                time += oneDay;
                continue;
            }
            time += oneDay;
        }

        List<Object> list = new ArrayList<>();
        String[] x = new String[timeList.size()];
        timeList.toArray(x);
        BigDecimal[] y1 = new BigDecimal[totalSumList.size()];
        totalSumList.toArray(y1);
        BigDecimal[] y2 = new BigDecimal[totalPriceList.size()];
        totalPriceList.toArray(y2);
        BigDecimal[] y3 = new BigDecimal[unitPriceList.size()];
        unitPriceList.toArray(y3);
        Integer[] y4 = new Integer[totalCountList.size()];
        totalCountList.toArray(y4);

        List<DataVo> list1 = mallOrderMapper.selectProductQuantity(StartTime, EndTime);

        List<DataVo> list2 = mallOrderMapper.selectProductPrice(StartTime, EndTime);

        List<DataVo> list3 = mallOrderMapper.selectProductQuantity2(StartTime, EndTime);

        List<DataVo> list4 = mallOrderMapper.selectProductPrice2(StartTime, EndTime);

        Calendar calendar1 = Calendar.getInstance();

        final int start = calendar1.getActualMinimum(Calendar.DAY_OF_MONTH);
        calendar1.set(Calendar.DAY_OF_MONTH, start);
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MILLISECOND, 0);

        Date startTime2 = calendar1.getTime();
        final int end = calendar1.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar1.set(Calendar.DAY_OF_MONTH, end);
        calendar1.set(Calendar.HOUR_OF_DAY, 23);
        calendar1.set(Calendar.MINUTE, 59);
        calendar1.set(Calendar.SECOND, 59);
        calendar1.set(Calendar.MILLISECOND, 999);

        Date endTime2 = calendar1.getTime();

        String count1 = mallOrderMapper.selectCount1(startTime2, endTime2).toString();

        String count2 = (mallOrderMapper.selectCount2(startTime2, endTime2).divide(new BigDecimal(10000), 2, ROUND_HALF_DOWN)).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString();

        String count3 = mallOrderMapper.selectCount3(startTime2, endTime2).toString();

        String count4 = (mallOrderMapper.selectCount4(startTime2, endTime2).divide(new BigDecimal(1000), 2, ROUND_HALF_DOWN)).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString();

        List<String> list5 = new ArrayList<>();
        list5.add(count1);
        list5.add(count2);
        list5.add(count3);
        list5.add(count4);

        list.add(x);
        list.add(y1);
        list.add(y2);
        list.add(y3);
        list.add(y4);
        list.add(list1);
        list.add(list2);
        list.add(list3);
        list.add(list4);
        list.add(list5);
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @GetMapping("getTotalBiao2")
    @ApiOperation(value = "获取用户分析总表")
    public ServerResponse<List<Object>> getTotalBiao2(String start_time, String end_time) throws Exception {
        //时间转换
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date StartTime = null;
        Date EndTime = null;
        if (start_time != null && !start_time.equals("") && end_time != null && !end_time.equals("")) {
            StartTime = sdf.parse(start_time);
            EndTime = sdf.parse(end_time);
        }

        Calendar calendar = Calendar.getInstance();
        assert StartTime != null;
        calendar.setTime(StartTime);

        Long startTime = calendar.getTimeInMillis();
        assert EndTime != null;
        calendar.setTime(EndTime);
        Long endTime = calendar.getTimeInMillis();
        Long oneDay = 1000 * 60 * 60 * 24L;

        Long time = startTime;

        List<String> timeList = new ArrayList<>();
        List<Integer> pvList = new ArrayList<>();
        List<Integer> uvList = new ArrayList<>();
        List<Double> conversionList = new ArrayList<>();
        while (time <= endTime) {
            Date d = new Date(time);
            calendar.setTime(d);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            Date oneDayStart = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
            Date oneDayEnd = calendar.getTime();
            List<MallOrder> orderList = mallOrderMapper.selectOrderByTime(oneDayStart, oneDayEnd);
            Integer pvCount = biUserAnalysisMapper.selectPVCount(oneDayStart, oneDayEnd);
            Integer uvCount = biUserAnalysisMapper.selectUVCount(oneDayStart, oneDayEnd);
            if (pvCount != 0 || uvCount != 0) {
                timeList.add(calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日");
                System.out.println("*************************");
                pvList.add(pvCount);
                uvList.add(uvCount);
                Double conversion = 0.00;
                if (uvCount != 0) {
                    conversion = (double) orderList.size() / uvCount;
                }
                conversionList.add(conversion);
            } else {
                time += oneDay;
                continue;
            }
            time += oneDay;
        }

        List<Object> list = new ArrayList<>();
        String[] x = new String[timeList.size()];
        timeList.toArray(x);
        Integer[] y1 = new Integer[pvList.size()];
        pvList.toArray(y1);
        Integer[] y2 = new Integer[uvList.size()];
        uvList.toArray(y2);
        Double[] y3 = new Double[conversionList.size()];
        conversionList.toArray(y3);

        List<DataVo2> list1 = mallOrderMapper.selectDeviceTypeCountList(StartTime, EndTime);

        List<DataVo2> list2 = mallOrderMapper.selectPurchaseTypeCountList(StartTime, EndTime);

        List<DataVo2> list3 = biUserAnalysisMapper.selectDeviceList(StartTime, EndTime);

        List<Integer> list4 = new ArrayList<>();

        Integer count1 = 0;

        for (Integer aY1 : y1) {
            count1 = count1 + aY1;
        }

        Integer count2 = 0;

        for (Integer aY2 : y2) {
            count2 = count2 + aY2;
        }

        Integer count3 = 0;
        Integer count4 = 0;
        for (DataVo2 aList1 : list1) {
            if (aList1.getName().equals("PC")) {
                count4 = count4 + aList1.getValue();
            } else {
                count3 = count3 + aList1.getValue();
            }
        }

        list4.add(count1);
        list4.add(count2);
        list4.add(count3);
        list4.add(count4);


        list.add(x);
        list.add(y1);
        list.add(y2);
        list.add(y3);
        list.add(list1);
        list.add(list2);
        list.add(list3);
        list.add(list4);
        return ServerResponse.createBySuccess(list);
    }


}
